<template>
  <a-modal
    :open="open"
    width="640px"
    :title="rowData?.id ? '修改医保终端' : '新增医保终端'"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="终端ID" class="w-1/2" v-bind="validateInfos.termId">
        <a-input v-model:value="formState.termId" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="名称" class="w-1/2">
        <a-input v-model:value="formState.termName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="IP" class="w-1/2">
        <a-input v-model:value="formState.ip" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="MAC地址" class="w-1/2" v-bind="validateInfos.mac">
        <a-input v-model:value="formState.mac" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="4" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  termId: '',
  mac: ''
})
const rulesState = ref({
  termId: [{ required: true, message: '请输入终端ID' }],
  mac: [{ required: true, message: '请输入MAC地址' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const res = await post('/operation/opex/doSaveTerminaalConfig', formState.value)
  res.code !== 0 ? message.error(res.msg || '操作失败') : message.success('保存成功')
  emit('success')
  onCancel()
}
watch(
  () => props.open,
  (val) => {
    if (val && props.rowData?.id) {
      formState.value = { ...props.rowData }
    }
  },
  {
    immediate: true
  }
)

const onCancel = () => {
  resetFields()
  emit('update:open', false)
}
</script>
